package wzc.design_pattern.creation.prototype.deep_copy;

import java.io.*;
import java.util.List;

/**
 * @Author 王振川
 * @Date 2025/7/19
 */

public class PersonSerializable implements Serializable {
    String name;
    List<String> hobbies;

    public PersonSerializable(String name, List<String> hobbies) {
        this.name = name;
        this.hobbies = hobbies;
    }

    // 使用序列化进行深拷贝
    public PersonSerializable deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (PersonSerializable) ois.readObject();
    }
}
